package progressBugBusiness;

import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.UUid;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/2/22.
 * x
 */
public class SaveProgressBug implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //数据库操作
        System.out.println(requestInfo);
        String action = requestInfo.getString("action");

        //新建条目
        //INSERT INTO `progresscase` (`uuid`, `progressname`, `module`, `title`, `condition`, `steps`, `state`) VALUES
        // ('regfgfd234343', '口福3.4', '首页', '这个是标题', '这个是前置条件', '这个操作步骤', '1')
        if (Objects.equals("addBug", action)){
            JSONObject data = requestInfo.getJSONObject("data");
            System.out.println(data);
            String id = data.getString("id");
            String uuid = UUid.getUUid();
            String progressname = data.getString("progressname");
            String module = data.getString("module");
            String owner = data.getString("owner");
            String first = data.getString("first");
            String title = data.getString("title");
            String describe = data.getString("describe");
            String state = "1";
            String picarr = data.getString("picarr");
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            SqlReturnModel sqlReturnModel = new SqlReturnModel();
            if (Objects.equals(id, "")){
                String value = "('"+ uuid + "', '"+progressname+"', '"+module+"', '"+owner+"', '"+first+"', '"+title+"', '"+describe+"', '"+state+"', '"+picarr+"')";
                String insertSql = "INSERT INTO `progressbug` (`uuid`, `progressname`, `module`, `owner`, `first`, `title`, `describe`, `state`, `picarr`) VALUES " + value;
                sqlReturnModel = sqlDispatch.insert(insertSql);
            }else {
                //这里是编辑bug更新数据
                String sqlset = "`progressname`='" + progressname + "',";
                sqlset = sqlset + "`module`='" + module + "',";
                sqlset = sqlset + "`owner`='" + owner + "',";
                sqlset = sqlset + "`first`='" + first + "',";
                sqlset = sqlset + "`title`='" + title + "',";
                sqlset = sqlset + "`describe`='" + describe + "',";
                sqlset = sqlset + "`picarr`='" + picarr + "'";
                String sqlWhere = " WHERE uuid = '"+id+"'";

                String updateSql = "UPDATE progressbug SET " + sqlset + sqlWhere;
                System.out.println(updateSql);
                sqlReturnModel = sqlDispatch.update(updateSql);
            }

            returnJson.setResult("");
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        //save 更新bug状态
        if (Objects.equals("update", action)){
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            JSONObject data = requestInfo.getJSONObject("data");
            String uuid = data.getString("uuid");
            String state = "";
            if (!Objects.equals(data.getString("state"), null)){
                state = "`state`='" + data.getString("state") + "',";
            }
            String describe = "";
            if (!Objects.equals(data.getString("describe"), null)){
                describe = "`describe`='" + data.getString("describe") + "',";
            }
            String updateSql = "UPDATE progressbug SET ";
            String sqlset = state + describe ;
            sqlset = sqlset.substring(0, sqlset.length()-1);
            updateSql = updateSql + sqlset + " WHERE uuid = '"+uuid+"'";
            System.out.println(updateSql);
            SqlReturnModel sqlReturnModel = sqlDispatch.update(updateSql);
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }


        return returnJson.toString();
    }
}
